package com.socio.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;

import org.apache.http.client.ClientProtocolException;

import android.net.Uri;

public class SocioUtils {
	
	private static final int NUM_DAY_SEC = 86400000;
	
	/**
	 * Return JSON array in string form from passed in URL
	 * @param url url to connect to
	 * @return JSON
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String getResponseFromURL(Uri.Builder uriBuilder) throws Exception {
		
		// setup connection to URL
		URL url = new URL(uriBuilder.build().toString());
		HttpURLConnection conn = (HttpURLConnection)url.openConnection();
		conn.setConnectTimeout(60000);
		conn.setRequestMethod("GET");
		conn.connect();
		
		// get results from url
		BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
		StringBuilder json = new StringBuilder();
		String line = "";
		while ((line = rd.readLine()) != null) {
			json.append(line);
		}
		
		return json.toString();
	}
	
	/**
	 * POST JSON to url
	 * @param uriBuilder
	 * @param request
	 * @return if post was successful
	 * @throws Exception if anything goes wrong
	 */
	public static String postStringToURL(Uri.Builder uriBuilder, String request) throws Exception {
		
		// setup connection to URL
		URL url = new URL(uriBuilder.build().toString());
		HttpURLConnection conn = (HttpURLConnection)url.openConnection();
		conn.setConnectTimeout(60000);
		conn.setRequestMethod("POST");
		conn.setDoOutput(true);
		conn.setRequestProperty( "Content-type", "application/json" );
		conn.setRequestProperty( "Content-length", Integer.toString(request.length()));
		
		// write json to output stream
		PrintWriter out = new PrintWriter(conn.getOutputStream());
		out.print(request);
		out.flush();
		out.close();
		
		// get results from url
		BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
		StringBuilder response = new StringBuilder();
		String line = "";
		while ((line = rd.readLine()) != null) {
			response.append(line);
		}
		
		return response.toString();
	}
	
	/**
	 * Get the time that represents today
	 * @return todays time
	 */
	public static long getTodayTime() {
		return getDayTimeContainingTime(System.currentTimeMillis());
	}
	
	/**
	 * Get the time that represents yesterday
	 * @param time today's time
	 * @return yesterdays time
	 */
	public static long previousDayFromTime(long time) {
		long dayTimeOfParamater = getDayTimeContainingTime(time);
		return dayTimeOfParamater - NUM_DAY_SEC;
	}
	
	/**
	 * Get the time that represents the equivalent of tomorrow
	 * @param time today's time
	 * @return tomorrow's time
	 */
	public static long nextDayFromTime(long time) {
		long dayTimeOfParamater = getDayTimeContainingTime(time);
		return dayTimeOfParamater + NUM_DAY_SEC;
	}
	
	/**
	 * Pass in any time and get the day time that represents it
	 * @param time any time
	 * @return corresponding day time
	 */
	public static long getDayTimeContainingTime(long time) {
		return time - (time % NUM_DAY_SEC);
	}
	
	/**
	 * Get day string in form of DD/MM/YYYY that represents the time passed in
	 * @param time time to be converted
	 * @return DD/MM/YYYY
	 */
	public static String dayTimeToString(long time) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date(time));
		
		String dateString = ""+ (calendar.get(Calendar.MONTH)+1); 
		dateString += "/"+ calendar.get(Calendar.DAY_OF_MONTH);
		dateString += "/"+ calendar.get(Calendar.YEAR);

		return dateString;
	}
}
